package cn.cvs.controller;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.Supplier;
import cn.cvs.pojo.SysRole;
import cn.cvs.service.StorageRecordService;
import cn.cvs.service.SupplierService;
import cn.cvs.service.SysRoleService;
import cn.cvs.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/storageRecord")
public class StorageRecordController {
    private Logger logger = Logger.getLogger(StorageRecordController.class);

    @Resource
    private StorageRecordService storageRecordService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private SysRoleService sysRoleService;

    @GetMapping("/list")
    public String list(Model model, @RequestParam(value = "queryGoodsName", required = false) String queryGoodsName,
                       @RequestParam(value = "querySupplierId", required = false) Integer querySupplierId,
                       @RequestParam(value = "queryPayStatus", required = false) Integer queryPayStatus,
                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex) {
        List<Supplier> supplierList = null;
        List<StorageRecord> storageRecordList = null;
        try {
            PageHelper.startPage(pageIndex, 5);
            StorageRecord storageRecord = new StorageRecord(queryGoodsName, querySupplierId, queryPayStatus);
            storageRecordList = this.storageRecordService.getList(storageRecord);
            supplierList = supplierService.getList();

            PageInfo<StorageRecord> page = new PageInfo<>(storageRecordList);
            model.addAttribute("supplierList",supplierList);
            model.addAttribute("queryGoodsName", queryGoodsName);
            model.addAttribute("querySupplierId", querySupplierId);
            model.addAttribute("queryPayStatus", queryPayStatus);
            model.addAttribute("page", page);
            return "storageRecord/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/toSysError";
        }
    }

    // 跳转到异常处理页面
    @RequestMapping(value = "/toSysError")
    public String sysError(){
        return "sysError";
    }

    // 查看详情
    @GetMapping("/view/{id}")
    public String view(@PathVariable Integer id, Model model) throws Exception {
        StorageRecord storageRecord = storageRecordService.getById(id);
        model.addAttribute(storageRecord);
        return "storageRecord/view";
    }

    // 跳转到添加页面
    @GetMapping("/toAdd")
    public String toAdd(Model model){
        List<SysRole> sysRoleList = new ArrayList<>();
        try{
            sysRoleList = sysRoleService.getRoleList();
            model.addAttribute("sysRoleList",sysRoleList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "storageRecord/add";
    }

    // 添加
    @PostMapping("/add")
    public String add(StorageRecord storageRecord, HttpSession session) throws Exception {
        storageRecord.setCreatedUserId(((StorageRecord)session.getAttribute(Constants.USER_SESSION)).getId());
        if (storageRecordService.add(storageRecord)){
            return "redirect:/storageRecord/list";
        }
        return "redirect:/add";
    }
}
